package com.hebrf.sdk.dialog;

import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.hebrf.sdk.qrcode.zxing.encode.EncodingHandler;

import java.io.UnsupportedEncodingException;


/**
 * Created by lyb on 2017/6/14.
 * Email: 1373519715@qq.com
 * Blog: https://littleiii.github.io
 */

public class QrcodeDialog implements BaseDialog {
    private AlertDialog mAD;
    private View mView;
    private String content;
    private TextView tvContent;
    private TextView tvYes;
    private ImageView iv2Code;
    private Bitmap qrCode;

    public QrcodeDialog(Activity activity) {
        mView = View.inflate(activity, 0, null);
        mAD = new AlertDialog.Builder(activity).
                setView(mView).
                create();
        tvContent = (TextView) mView.findViewById(0);
        iv2Code = (ImageView) mView.findViewById(0);
        tvYes = (TextView) mView.findViewById(0);
        tvYes.setOnClickListener((view) -> {
            cancel();

        });
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public void setContent(String s) {
        content = s;
        try {
            qrCode = EncodingHandler.create2Code(s, 400);
            iv2Code.setImageBitmap(qrCode);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public AlertDialog getDialog() {
        return mAD;
    }

    @Override
    public void show() {
        if (mAD == null) {
            return;
        }
        mAD.show();
    }

    @Override
    public void cancel() {
        if (qrCode != null) qrCode = null;
        if (mAD == null) {
            return;
        }
        mAD.cancel();
    }


}
